因為課堂需要需要研究android連線到arduino透過藍芽的方式,所以開始研究藍芽的使用方法
一開始先幫藍芽做連線確認
class MainActivity : AppCompatActivity() {
companion object{
fun EXTRA_ADDRESS() = "device_address"
}
private var myBlueTooth: BluetoothAdapter? = null
private val pairedDevices: Set<*>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this,R.layout.activity_main)
myBlueTooth = BluetoothAdapter.getDefaultAdapter()
if (myBlueTooth == null){
Toast.makeText(applicationContext, "Bluetooth Device Not Available", Toast.LENGTH_LONG).show()
}
else{
if (myBlueTooth!!.isEnabled){
}
else{
val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
startActivityForResult(intent,1)
}
}
binding.button.setOnClickListener { pairedDeviceList() }
}
那這邊的companion object就是kotlin要宣告靜態變數的方式
接著是連線到藍芽的方式
private fun pairedDeviceList() {
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this,R.layout.activity_main)
val pairedDevices = myBlueTooth?.bondedDevices
val list = ArrayList<Any>()
if (pairedDevices!!.size > 0) {
for (bt in pairedDevices) {
list.add(bt.name + "\n" + bt.address) //Get the device's name and the address
}
} else {
Toast.makeText(
applicationContext,
"No Paired Bluetooth Devices Found.",
Toast.LENGTH_LONG
).show()
}
val adapter: ArrayAdapter<*> = ArrayAdapter(this, android.R.layout.simple_list_item_1, list)
binding.listView.adapter = adapter
val myListClickListener =
OnItemClickListener { av, v, arg2, arg3 -> // Get the device MAC address, the last 17 chars in the View
val info = (v as TextView).text.toString()
val address = info.substring(info.length - 17)
// Make an intent to start next activity.
val i = Intent(this, LedControl::class.java)
//Change the activity.
i.putExtra(
EXTRA_ADDRESS(),
address
) //this will be received at ledControl (class) Activity
startActivity(i)
}
binding.listView.onItemClickListener = myListClickListener //Method called when the device from the list is clicked
}
}private fun pairedDeviceList() {
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this,R.layout.activity_main)
val pairedDevices = myBlueTooth?.bondedDevices
val list = ArrayList<Any>()
if (pairedDevices!!.size > 0) {
for (bt in pairedDevices) {
list.add(bt.name + "\n" + bt.address) //Get the device's name and the address
}
} else {
Toast.makeText(
applicationContext,
"No Paired Bluetooth Devices Found.",
Toast.LENGTH_LONG
).show()
}
val adapter: ArrayAdapter<*> = ArrayAdapter(this, android.R.layout.simple_list_item_1, list)
binding.listView.adapter = adapter
val myListClickListener =
OnItemClickListener { av, v, arg2, arg3 -> // Get the device MAC address, the last 17 chars in the View
val info = (v as TextView).text.toString()
val address = info.substring(info.length - 17)
// Make an intent to start next activity.
val i = Intent(this, LedControl::class.java)
//Change the activity.
i.putExtra(
EXTRA_ADDRESS(),
address
) //this will be received at ledControl (class) Activity
startActivity(i)
}
binding.listView.onItemClickListener = myListClickListener //Method called when the device from the list is clicked
}
}
這邊我有用databinding,但是還沒連結到viewmodel等功能做齊全後會設法移到viewmodel